#include "AutoMobile.h"
#include "GameManager.h"
#include "CEMoveBySpeed.h"
#include "Road.h"

AutoMobile::AutoMobile()
{
}

AutoMobile::~AutoMobile()
{
}

bool AutoMobile::init(){
    Vehicle::init();
    return true;
}

void AutoMobile::update(float delta){
    Vehicle::update(delta);
    
    if(delta > TIME_NOT_UPDATE)
        return;
    
    if(road == 0) {
        return;
    }
    /*if(isBesideSafe()){
     noTurn();
     }*/
    
    // check car not in last road
    if(getPositionX() < road->getContentSize().width - GameManager::getInstance()->preventCarTurnLaneBeforeCrossRoadDistance){
        checkTurnLane();
    }
    
    checkTurnDone();
}

void AutoMobile::turnRight(float horizontalTarget){
    horizontalDirection = HD_RIGHT;
    v.y = -calculateVTurn();
    this->horizontalTarget = horizontalTarget;
    numTurn++;
}
void AutoMobile::turnLeft(float horizontalTarget){
    horizontalDirection = HD_LEFT;
    v.y = calculateVTurn();
    this->horizontalTarget = horizontalTarget;
    numTurn++;
}

CCRect AutoMobile::getLeftSafeRect(){
    return leftSafeRectNextLane;
}
CCRect AutoMobile::getRightSafeRect(){
    return rightSafeRectNextLane;
}

void AutoMobile::checkTurnLane(){
    if(isSafe == false && horizontalDirection == HD_NONE){
        
        // check turn right
        bool isRightCanTurn = true;
        
        if(rightSafeRectNextLane.origin.y > 0){    // in road when turn right
            
            CCObject* it;
            CCARRAY_FOREACH(road->getVehicles(), it){
                Vehicle* vehicle = (Vehicle*)it;
                if(isInInteractableRange(vehicle)){
                    if(rightSafeRectNextLane.intersectsRect(vehicle->boundingBox())
                       || (
                           vehicle->isSafe == false
                           && (
                               rightSafeRectNextLane.intersectsRect(vehicle->getLeftSafeRectNextLane())
                               || rightSafeRectNextLane.intersectsRect(vehicle->getLeftSafeRectBeside())
                               )
                           )
                       ){
                        isRightCanTurn = false;
                        break;
                    }
                }
            }
            
            if(isRightCanTurn){
                turnRight(getPositionY() - road->getLaneWidth());
            }
        }
        else isRightCanTurn = false;
        
        // check turn left
        if(isRightCanTurn == false){
            
            // if vehicle in road when turn left and check can turn bus lane if in near bus lane
            if(leftSafeRectNextLane.getMaxY() < road->getRoadWidth() && road->checkVehicleCanTurnBusLaneMode(leftSafeRectNextLane, getTag())){
                
                CCObject* it;
                bool isLeftCanTurn = true;
                CCARRAY_FOREACH(road->getVehicles(), it){
                    Vehicle* vehicle = (Vehicle*)it;
                    if(isInInteractableRange(vehicle)){
                        if(leftSafeRectNextLane.intersectsRect(vehicle->boundingBox())
                           || (
                               vehicle->isSafe == false
                               && (
                                   leftSafeRectNextLane.intersectsRect(vehicle->getRightSafeRectNextLane())
                                   || leftSafeRectNextLane.intersectsRect(vehicle->getRightSafeRectBeside()))
                               )
                           ){
                            isLeftCanTurn = false;
                            break;
                        }
                    }
                }
                
                if(isLeftCanTurn){
                    turnLeft(getPositionY() + road->getLaneWidth());
                }
            }
        }
    }
}

void AutoMobile::noTurn(){
    Vehicle::noTurn();
    
    updateLaneInfo();
}

void AutoMobile::updateLaneInfo(){
    int whichLane = road->getWhichLane(this);
    switch (whichLane)
    {
        case 1:
            if(isInLane1 == false){
                GameManager::getInstance()->numCarLane1++;
                isInLane1 = true;
            }
            break;
            
        case 2:
            if(isInLane2 == false){
                GameManager::getInstance()->numCarLane2++;
                isInLane2 = true;
            }
            break;
            
        case 3:
            if(isInLane3 == false){
                GameManager::getInstance()->numCarLane3++;
                isInLane3 = true;
            }
            break;
    }
}